import { CURRENT_ALBUM_ID } from "./constants.js";
let mediaList = [];
let systemKeyObj;
let albumItem;
let userInfo;
let deviceInfo;
let deviceAuthData;
let isDataloaded = false;
export async function initialData() {
  try {
    userInfo = await window
      .fetch("https://api.hificloud.net/v1/login", {
        method: "post",
        headers: {
          "Content-Type": "application/json",
          "Api-Version": "1.16",
        },
        body: JSON.stringify({ username: "18513080379", password: "11111111" }),
      })
      .then((res) => res.json())
      .then((res) => res.data);
    // console.log("userInfo", userInfo);

    const headers = {
      "Content-Type": "application/json",
      "Api-Version": "1.16",
      Authorization: `Bearer ${userInfo.token}`,
    };

    const publicIp = await fetch("https://api.hificloud.net/v1/ip", {
      method: "get",
      headers,
    })
      .then((res) => res.json())
      .then((res) => res.data.ip);

    const gropus = await fetch("https://api.hificloud.net/v1/app/groups", {
      method: "get",
      headers,
    })
      .then((res) => res.json())
      .then((res) => res.data);

    // console.log("gropus", gropus);

    deviceInfo = gropus[0].device;

    systemKeyObj = await fetch(
      `https://api.hificloud.net/v1/sy/key?did=${deviceInfo.id}`,
      {
        method: "get",
        headers,
      }
    )
      .then((res) => res.json())
      .then((res) => res.data);

    // console.log("systemKeyObj", systemKeyObj);

    const lanIP = await window.electronAPI.invoke("getLANIP");

    deviceAuthData = await fetch(`${deviceInfo.hostname}/v2/auth`, {
      method: "post",
      headers,
      body: JSON.stringify({
        uid: userInfo.uid,
        did: deviceInfo.id,
        app: {
          code: 20605,
          version: "2.6.5",
        },
        network: {
          ip: publicIp, // public-ip
          lan: lanIP,
        },
      }),
    })
      .then((res) => res.json())
      .then((res) => res.data);

    // console.log("deviceAuthData", deviceAuthData);

    albumItem = await fetch(
      `${deviceInfo.hostname}/v3/album/list?range=0,1000`,
      {
        method: "get",
        headers: {
          ...headers,
          Authorization: `Bearer ${deviceAuthData.token}`,
        },
      }
    )
      .then((res) => res.json())
      .then(
        (res) =>
          res.data.content.filter(
            (item) => item.albumId === CURRENT_ALBUM_ID
          )[0]
      );

    // console.log("albumItem", albumItem);
    mediaList = await fetch(
      `${deviceInfo.hostname}/v3/album/media/list?id=${albumItem.albumId}&range=0,10000`,
      {
        method: "get",
        headers: {
          ...headers,
          Authorization: `Bearer ${deviceAuthData.token}`,
        },
      }
    )
      .then((res) => res.json())
      .then((res) => res.data.content);

    isDataloaded = true;

    // console.log("mediaList", mediaList);
    return {
      mediaList,
      systemKeyObj,
      albumItem,
      userInfo,
      deviceInfo,
      deviceAuthData,
      isDataloaded,
    };
  } catch (error) {
    console.error("error", error);
    throw error;
  }
}

export async function getDataInfo() {
  if (!isDataloaded) {
    return initialData();
  }

  return {
    mediaList,
    systemKeyObj,
    albumItem,
    userInfo,
    deviceInfo,
    deviceAuthData,
    isDataloaded,
  };
}

export function resetData() {
  mediaList = [];
  systemKeyObj = null;
  albumItem = null;
  userInfo = null;
  deviceInfo = null;
  deviceAuthData = null;
  isDataloaded = false;
}
